package cn.player.media.timeshift;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import cn.player.R;
import cntv.player.core.util.Utils;
import cn.player.media.util.DateUtil;

public class RulerView extends View implements Runnable {
    private Context mContext;
    private int width;
    private TimeShiftParams rParams;
    private long beginTime;
    private Paint bitmapPaint;
    private LinearLayout.LayoutParams rulerLayoutParams;

    public RulerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public RulerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public RulerView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public RulerView(Context context, int width, long beginTime, TimeShiftParams rp) {
        super(context);
        this.mContext = context;
        this.rParams = rp;
        this.width = width;
        this.beginTime = beginTime;

        rulerLayoutParams = new LinearLayout.LayoutParams(width, LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(rulerLayoutParams);
        this.setDrawingCacheBackgroundColor(0xffff0000);
        bitmapPaint = new Paint();
        new Thread(this).start();
    }

    protected void onDraw(Canvas canvas) {
        int littleGap = rParams.littleGrid / rParams.dpToDate;//小间隔长度
        int bigGrid = rParams.bigGrid;//每一个大隔有多少个小隔 

        int scaleHeight = mContext.getResources().getDrawable(R.drawable.live_big_timeshift_progress_thumb).getMinimumHeight();//刻度区域的高度
//        int scaleHeight = context.getResources().getDimensionPixelOffset(R.dimen.time_shift_seek_layout_height); //刻度区域的高度
        int seekHeight = mContext.getResources().getDimensionPixelOffset(R.dimen.timeshift_seekbar_height);      //状态条seekbar的高   

//        LogUtil.i("zl","scaleHeight1 = " + scaleHeight);

        bitmapPaint.setStrokeWidth(1.5f);
        bitmapPaint.setColor(Color.GRAY);
        //画上下两要横线
        canvas.drawLine(0, 0, width, 0, bitmapPaint);
        canvas.drawLine(0, scaleHeight, width, scaleHeight, bitmapPaint);


        int scaleGap = mContext.getResources().getDimensionPixelOffset(R.dimen.timeshift_scale_gap);//横线与竖线之间的最小间隙
        int longLine = (scaleHeight - seekHeight) / 2 - 2 * scaleGap;//长刻度长度，上下各留出scaleGap长度空隙
        int shortLine = longLine * 2 / 3;//短刻度长度
//		LogUtil.i("zl","seekBarLayoutHeight = " + scaleHeight);
//		LogUtil.i("zl","seekBarHeight = " + seekHeight);
//		LogUtil.i("zl","longLine = " + longLine);


        int topCoord = (scaleHeight - seekHeight) / 2;
        int bottomCoord = topCoord + seekHeight;

        int number = (int) Math.ceil(width / littleGap);
        for (int i = 0; i < number; i++) {

            bitmapPaint.setStrokeWidth(1.5f);
            bitmapPaint.setColor(Color.GRAY);

            int x = littleGap * i;
            int scaleH = i % bigGrid == 0 ? longLine : shortLine;//刻度的长度

            //从seekbar上下各scaleGap处画 scaleH长度的直线
            canvas.drawLine(x, topCoord - scaleGap, x, topCoord - scaleGap - scaleH, bitmapPaint);//画seekbar上刻度，上下各留出scaleGap空隙
            canvas.drawLine(x, bottomCoord + scaleGap, x, bottomCoord + scaleGap + scaleH, bitmapPaint);//画seekbar下刻度，上下各留出scaleGap空隙

            bitmapPaint.setColor(Color.WHITE);
            bitmapPaint.setTextSize(mContext.getResources().getDimension(R.dimen.text_size_eight));
            int d = i * rParams.littleGrid;
            if ((i * rParams.littleGrid) % (24 * 60 * 60) == 0) {
                String text = DateUtil.getTimeString(beginTime + d * 1000, "MM月dd日");
                int textH = Utils.getStringBounds(bitmapPaint, text)[1];
                canvas.drawText(text, x, scaleHeight + textH + 10, bitmapPaint);
            } else if (i % rParams.bigGrid == 0) {
                String text = DateUtil.getTimeString(beginTime + d * 1000, "HH:mm");
                int textW = Utils.getStringBounds(bitmapPaint, text)[0];
                int textH = Utils.getStringBounds(bitmapPaint, text)[1];
                canvas.drawText(text, x - textW / 2, scaleHeight + textH + 10, bitmapPaint);
            }
        }
    }

    @Override
    public void run() {
        // TODOAuto-generated method stub
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO: handle exception
                Thread.currentThread().interrupt();
            }
            //使用postInvalidate 可以直接在线程中更新界面
            postInvalidate();
        }
    }

}
